import axios from 'axios'
import type {
  InternalAxiosRequestConfig,
  AxiosResponse
} from 'axios'

import project from '../../project-which'
const { api } = project

function TOKEN () {
  return localStorage[localStorage.getItem('tokenName') + '_token'] || ''
}


// axios.defaults.baseURL = "http://192.168.1.203:4300"

// 全局拦截器
axios.interceptors.request.use(
  (config: InternalAxiosRequestConfig) => {
    if (TOKEN()) {
      config.headers.Authorization = 'Bearer ' + TOKEN()
    }
    // 绑定真实API
    config.url = api[config.url!] || config.url
    return config
  },
  (err: any) => {
    return Promise.reject(err)
  }
)

axios.interceptors.response.use(
  (res: AxiosResponse) => {
    let result = res.data || res
    // .... 可以在这里设置token过期后的操作
    return result
  },
  (err: any) => {
    return Promise.reject(err)
  }
)
